package com.example.test_umeng; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.Toast; import com.umeng.socialize.bean.SHARE_MEDIA; import com.umeng.socialize.bean.SocializeEntity; import com.umeng.socialize.controller.UMServiceFactory; import com.umeng.socialize.controller.UMSocialService; import com.umeng.socialize.controller.listener.SocializeListeners.SnsPostListener; import com.umeng.socialize.media.QQShareContent; import com.umeng.socialize.media.QZoneShareContent; import com.umeng.socialize.media.UMImage; import com.umeng.socialize.sso.QZoneSsoHandler; import com.umeng.socialize.sso.SinaSsoHandler; import com.umeng.socialize.sso.SmsHandler; import com.umeng.socialize.sso.TencentWBSsoHandler; import com.umeng.socialize.sso.UMQQSsoHandler; import com.umeng.socialize.weixin.controller.UMWXHandler; import com.umeng.socialize.weixin.media.CircleShareContent; import com.umeng.socialize.weixin.media.WeiXinShareContent; public class MainActivity extends Activity implements OnClickListener { private Button button; // 首先在您的Activity中添加如下成员变量 final UMSocialService mController = UMServiceFactory .getUMSocialService("com.umeng.share"); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); init(); } private void init() { // 设置分享内容 mController .setShareContent("友盟社会化组件(SDK)让移动应用快速整合社交分享功能,http://www.umeng.com/social"); // 设置分享图片, 参数2为图片的url地址 mController.setShareMedia(new UMImage(this, R.drawable.shar_img)); //-------------------------------------------------------------------------------------------------------- // 参数1为当前Activity,参数2为开发者在QQ互联申请的APP ID,参数3为开发者在QQ互联申请的APP kEY.(自己申请) UMQQSsoHandler qqSsoHandler = new UMQQSsoHandler(this, "801556848", "5bc9c2b47e38eb5ab50107193e8dce1a"); qqSsoHandler.addToSocialSDK(); QQShareContent qqShareContent = new QQShareContent(); // 设置分享文字 qqShareContent.setShareContent("来自友盟社会化组件(SDK)让移动应用快速整合社交分享功能 -- QQ"); // 设置分享title qqShareContent.setTitle("hello, title"); // 设置分享图片 qqShareContent.setShareImage(new UMImage(this, R.drawable.shar_img)); // 设置点击分享内容的跳转链接 qqShareContent.setTargetUrl("http://www.baidu.com"); mController.setShareMedia(qqShareContent); //-------------------------------------------------------------------------------------------------------- // 参数1为当前Activity,参数2为开发者在QQ互联申请的APP ID,参数3为开发者在QQ互联申请的APP kEY.(自己申请) QZoneSsoHandler qZoneSsoHandler = new QZoneSsoHandler(this, "801556848", "5bc9c2b47e38eb5ab50107193e8dce1a"); qZoneSsoHandler.addToSocialSDK(); QZoneShareContent qzone = new QZoneShareContent(); // 设置分享文字 qzone.setShareContent("来自友盟社会化组件(SDK)让移动应用快速整合社交分享功能 -- QZone"); // 设置点击消息的跳转URL qzone.setTargetUrl("http://www.baidu.com"); // 设置分享内容的标题 qzone.setTitle("QZone title"); // 设置分享图片 qzone.setShareImage(new UMImage(this, R.drawable.shar_img)); mController.setShareMedia(qzone); //-------------------------------------------------------------------------------------------------------- // 设置腾讯微博SSO handler mController.getConfig().setSsoHandler(new TencentWBSsoHandler()); //-------------------------------------------------------------------------------------------------------- // 设置新浪SSO handler mController.getConfig().setSsoHandler(new SinaSsoHandler()); //-------------------------------------------------------------------------------------------------------- // 添加微信的appID appSecret要自己申请 String appID = "wx9cdd87b0fb07579b"; String appSecret = "e5af270341665a1b2d558205d0bb5b17"; // 添加微信平台 UMWXHandler wxHandler = new UMWXHandler(this, appID, appSecret); wxHandler.addToSocialSDK(); // 设置微信好友分享内容 WeiXinShareContent weixinContent = new WeiXinShareContent(); // 设置分享文字 weixinContent.setShareContent("来自友盟社会化组件(SDK)让移动应用快速整合社交分享功能,微信"); // 设置title weixinContent.setTitle("友盟社会化分享组件-微信"); // 设置分享内容跳转URL weixinContent.setTargetUrl("http://www.baidu.com"); // 设置分享图片 weixinContent.setShareImage(new UMImage(getApplicationContext(), R.drawable.ic_launcher)); mController.setShareMedia(weixinContent); //-------------------------------------------------------------------------------------------------------- // 添加微信朋友圈(自会显示title,不会显示内容,官网这样说的) UMWXHandler wxCircleHandler = new UMWXHandler(this, appID, appSecret); // 设置微信朋友圈分享内容 CircleShareContent circleMedia = new CircleShareContent(); circleMedia.setShareContent("来自友盟社会化组件(SDK)让移动应用快速整合社交分享功能,朋友圈"); // 设置朋友圈title circleMedia.setTitle("友盟社会化分享组件-朋友圈"); circleMedia.setShareImage(new UMImage(getApplicationContext(), R.drawable.ic_launcher)); circleMedia.setTargetUrl("http://www.baidu.com"); mController.setShareMedia(circleMedia); wxCircleHandler.setToCircle(true); wxCircleHandler.addToSocialSDK(); //-------------------------------------------------------------------------------------------------------- // 添加短信 SmsHandler smsHandler = new SmsHandler(); smsHandler.addToSocialSDK(); //-------------------------------------------------------------------------------------------------------- button = (Button) findViewById(R.id.btn_share); button.setOnClickListener(this); } @Override public void onClick(View arg0) { switch (arg0.getId()) { case R.id.btn_share: mController.getConfig().removePlatform(SHARE_MEDIA.RENREN, SHARE_MEDIA.DOUBAN); //默认分享方式 mController.openShare(this, false); break; } } }
评论